const base = '';

let flag = false;

let PAGE = 1;

// 操作按钮
$('#control').on('click', function () {
    if ($(this).text() === '操作') {
        $('.check').show()
        $(this).text('取消')
    } else {
        $('.check').hide()
        $(this).text('操作')
    }

})

// 删除按钮
$('#del').on('click', function () {
    let dataArray = getCheckID()
    let len = dataArray.length;
    dataArray.forEach(function (ID, i) {
        $.ajax({
            url: base + '/tour/delete',
            type: 'delete',
            dataType: 'JSON',
            data: {
                id: ID
            },
            headers: {
                authorization: localStorage.getItem('token')
            },
            success: function (msg) {
                if (i !== len - 1) return
                // 从dom节点中删除列表
                removeList();
            }
        })
    })

})

// 获取选中的input
const getCheckID = function () {
    let input = $('.check:checked')
    let data = [];
    input.each(function (i, el) {
        data.push($(el).attr('data-id'))
    })
    return data
}

// 删除选中的list
const removeList = function () {
    $('.check:checked').parent().remove()
    popup()
}

// 滚动加载数据
$(window).scroll(function () {
    let top = $(window).scrollTop();
    let height = $(window).height();
    let tall = $(document).height();
    if (top + height === tall) {
        PAGE++
        fresh(PAGE)
    }
})

// 登录注册页面
// Window: hashchange event
window.addEventListener('hashchange', function () {
    let hash = location.hash.slice(1);
    if (hash) {
        route[hash]()
    } else {
        console.log('返回首页')
    }

})

// 路由定义
const route = {
    login: function () {
        templa('登录', 'login')
    },
    sign: function () {
        templa('注册', 'sign')
    }
}

// 加载模板
function templa(id, cla) {
    let data = {
        id: id,
        name: cla
    }
    let html = template('loginTpl', data)

    let div = document.createElement('div');
    div.className = 'login'
    div.innerHTML = html;
    document.body.append(div)

    // 事件绑定
    $('#back').on('click', comingBack)
    // 登录
    $('#login') && $('#login').on('click', onLogin)
    // 注册
    $('#sign') && $('#sign').on('click', onSign)
}

// 返回
function comingBack() {
    $('#back').unbind('click', comingBack)
    // 登录
    $('#login') && $('#login').unbind('click', onLogin)
    // 注册
    $('#sign') && $('#sign').unbind('click', onSign)
    $('.login').remove()
    history.back()
}

// 登录事件
function onLogin() {
    let data = getDAta()
    $.ajax({
        type: 'POST',
        datatype: 'JSON',
        url: base + '/user/login',
        data: {
            username: data.user,
            password: data.psw
        },
        success: function (msg) {
            let status = msg.status;
            if (status === 200) {
                localStorage.setItem('token', `Bearer ${msg.data.token}`)
                flag = true;
                fresh()
                popup('登录成功')
            } else {
                console.log('登录失败')
            }
        }

    })
}

// 注册事件
function onSign() {
    let data = getDAta()
    $.ajax({
        type: "POST",
        datatype: "JSON",
        url: base + '/user/registary',
        data: {
            username: data.user,
            password: data.psw
        },
        success: function (msg) {
            console.log(msg)
            popup('注册成功')
        }
    })
}

// 收集用户提交数据
function getDAta() {
    let data = {};
    $('.weui-input').each(function (i, el) {
        let key = $(el).attr('name')
        data[key] = $(el).val()
    })
    return data
}

// 加载新闻列表页
const fresh = function (PAGE) {
    $.ajax({
        url: base + '/tour/lists',
        type: 'GET',
        dataType: 'JSON',
        data: {
            page: PAGE,
            pageSize: 15
        },
        headers: {
            authorization: localStorage.getItem('token')
        },
        success: function (msg) {
            render(msg.data)
        }
    })

}

// 渲染
function render(data) {
    let fragment = new DocumentFragment();
    $('#LoginToRemind').hide()
    data.forEach(function (el, i) {
        let link = el.link;
        let a = document.createElement('a')
        a.className = 'weui-media-box weui-media-box_appmsg';
        a.href = link;
        let html = template('newList', el)
        a.innerHTML = html;
        fragment.append(a)
    });

    $('.weui-panel__bd').append(fragment)
    if (flag === true) {
        $('.button-sp-area>a').removeClass('weui-btn_disabled')
    } else {
        $('.button-sp-area>a').addClass('weui-btn_disabled')
    }

}

function popup() {
    let Dialog = $('#toast')
    Dialog.show()
    setTimeout(function () {
        Dialog.hide()
    }, 800)
}
